package com.soyoung.controller;

import com.soyoung.config.OrderClient;
import com.soyoung.config.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : zhq
 * @since : 2018/5/3
 */
@RestController
@RequestMapping("redis")
public class RedisController {
    //key 过期时间48小时
    private static final Integer seconds = 2*24*60*60;

    Logger log = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    RedisClient redisClient;

    //连接config-client-one客户端
    @Autowired
    private OrderClient orderClient;


    @RequestMapping("show/{key}")
    public String getRedis(@PathVariable("key") String key){
        String str = redisClient.get(key);
        String strs = key+"对应的value:"+str;
        log.info(strs);
        return strs;
    }

    /**
     * 读取config-client-one服务的信息
     * @param id
     * @return
     */
    @RequestMapping("get/{id}")
    public String showclient(@PathVariable("id") Object id){
        return orderClient.getbyid(id);
    }

    @RequestMapping(value = "save/{key}/{value}",method ={RequestMethod.GET,RequestMethod.POST})
    public String set2Redis(@PathVariable("key") String key,
                            @PathVariable("value") String value) throws Exception{
        redisClient.set(key,value,seconds);
        String str = redisClient.get(key);
        return str;
    }
}
